﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using CommunitySystem.Ctrls;

namespace CommunitySystem.Common
{
    public class InitialDictionary
    {
        public static Dictionary<string, DataService.DataDictionaryModel> _Dicts = new Dictionary<string, DataService.DataDictionaryModel>();

        public static Dictionary<string, DataService.DataDictionaryModel> Dict
        {
            get { return _Dicts; }
        }

        private DataService.DataServiceClient _client = null;
        public ObservableCollection<string> InitDict = new ObservableCollection<string>()
        {

        };

        public InitialDictionary()
        {
            #region 静态数据字典
            ///晋升方式, 晋升流程中使用
            if (!_Dicts.ContainsKey("PromotionType"))
            {
                _Dicts.Add("PromotionType", new DataService.DataDictionaryModel()
                {
                    DictCode = "PromotionType",
                    IsTree = false,
                    Item = new ObservableCollection<DataService.DataDictionaryItemModel>()
                {
                    new DataService.DataDictionaryItemModel(){ DictCode="Competition", DictValue="竞争上岗"},
                    new DataService.DataDictionaryItemModel(){ DictCode="Recommend", DictValue="组织推荐"},
                    new DataService.DataDictionaryItemModel(){ DictCode="Selection", DictValue="民主推荐"},
                    new DataService.DataDictionaryItemModel(){ DictCode="Selection", DictValue="选举"},
                }
                });
            }
            if (!_Dicts.ContainsKey("IsAgree"))
            {
                _Dicts.Add("IsAgree", new DataService.DataDictionaryModel()
                {
                    DictCode = "IsAgree",
                    IsTree = false,
                    Item = new ObservableCollection<DataService.DataDictionaryItemModel>()
                {
                    new DataService.DataDictionaryItemModel(){ DictCode="true", DictValue="同意"},
                    new DataService.DataDictionaryItemModel(){ DictCode="false", DictValue="不同意"},
                   
                }
                });
            }

            if (!_Dicts.ContainsKey("IsAgreePromot"))
            {
                _Dicts.Add("IsAgreePromot", new DataService.DataDictionaryModel()
                {
                    DictCode = "IsAgreePromot",
                    IsTree = false,
                    Item = new ObservableCollection<DataService.DataDictionaryItemModel>()
                {
                    new DataService.DataDictionaryItemModel(){ DictCode="true", DictValue="同意"},
                    new DataService.DataDictionaryItemModel(){ DictCode="false", DictValue="不同意晋升"},
                   
                }
                });
            }


            if (!_Dicts.ContainsKey("HasProblem"))
            {
                _Dicts.Add("HasProblem", new DataService.DataDictionaryModel()
                {
                    DictCode = "HasProblem",
                    IsTree = false,
                    Item = new ObservableCollection<DataService.DataDictionaryItemModel>()
                {
                    new DataService.DataDictionaryItemModel(){ DictCode="true", DictValue="有情况反映"},
                    new DataService.DataDictionaryItemModel(){ DictCode="false", DictValue="未收到情况反映"},
                   
                }
                });
            }

            if (!_Dicts.ContainsKey("CheGaiStatus"))
            {
                _Dicts.Add("CheGaiStatus", new DataService.DataDictionaryModel()
                {
                    DictCode = "CheGaiStatus",
                    IsTree = false,
                    Item = new ObservableCollection<DataService.DataDictionaryItemModel>()
                {
                    new DataService.DataDictionaryItemModel(){ DictCode="全部", DictValue="全部"},
                    new DataService.DataDictionaryItemModel(){ DictCode="已导出", DictValue="已导出"},
                    new DataService.DataDictionaryItemModel(){ DictCode="未导出", DictValue="未导出"}
                }
                });
            }

            if (!_Dicts.ContainsKey("CheGaiDengJiStatus"))
            {
                _Dicts.Add("CheGaiDengJiStatus", new DataService.DataDictionaryModel()
                {
                    DictCode = "CheGaiDengJiStatus",
                    IsTree = false,
                    Item = new ObservableCollection<DataService.DataDictionaryItemModel>()
                {
                    new DataService.DataDictionaryItemModel(){ DictCode="登记类", DictValue="登记类"},
                    new DataService.DataDictionaryItemModel(){ DictCode="变动类", DictValue="变动类"}
                }
                });
            }

            #endregion
            _client = new DataService.DataServiceClient();           
        }

        void _client_GetDictModelCompleted(object sender, DataService.GetDictModelCompletedEventArgs e)
        {
            if (!InitialDictionary._Dicts.ContainsKey(e.Result.DictCode))
                InitialDictionary._Dicts.Add(e.Result.DictCode, e.Result);
        }

        public void Init()
        {
            _client.GetDictModelCompleted += new EventHandler<DataService.GetDictModelCompletedEventArgs>(_client_GetDictModelCompleted);
            foreach (string dictcode in InitDict)
            {
                _client.GetDictModelAsync(dictcode, null);
            }
        }


        public void RefreshDictionary(string DictCode, string param, DictTextBox dtb)
        {
            _client.GetDictModelCompleted += (sender, e) =>
            {
                if (e.Error == null)
                {
                    if (InitialDictionary._Dicts.ContainsKey(e.Result.DictCode))
                        InitialDictionary._Dicts.Remove(e.Result.DictCode);
                    InitialDictionary._Dicts.Add(e.Result.DictCode, e.Result);
                    if (dtb != null)
                    {
                        dtb.client_GetDictModelCompleted(null, e);
                    }
                }
            };
            _client.GetDictModelAsync(DictCode, param);
        }
        public void RefreshDictionary(string DictCode, string param, DictComboBox dtb)
        {
            _client.GetDictModelCompleted += (sender, e) =>
            {
                if (e.Error == null)
                {
                    if (InitialDictionary._Dicts.ContainsKey(e.Result.DictCode))
                        InitialDictionary._Dicts.Remove(e.Result.DictCode);
                    InitialDictionary._Dicts.Add(e.Result.DictCode, e.Result);
                    if (dtb != null)
                    {
                        dtb.client_GetDictModelCompleted(null, e);
                    }
                }
            };
            _client.GetDictModelAsync(DictCode, param);
        }
    }
}
